package com.lsh.oms.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wuhao on 2019/6/14.
 */
public class StringTool {
    private static final Pattern linePattern = Pattern.compile("_(\\w)");

    private static Logger logger = LoggerFactory.getLogger(StringTool.class);
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 下划线转驼峰 */
    public static Map<String,Object> lineToHump(Map<String,Object> ori) {
        Map<String,Object> val = new HashMap<>();
        for (String key : ori.keySet()){
            Object value = ori.get(key);
            key = lineToHump(key);
            val.put(key,value);
        }
        return val;
    }


    public final static void convert(Object json) {
        if (json instanceof JSONArray) {
            JSONArray arr = (JSONArray) json;
            for (Object obj : arr) {
                convert(obj);
            }
        } else if (json instanceof JSONObject) {
            JSONObject jo = (JSONObject) json;
            Set<String> keys = jo.keySet();
            String[] array = keys.toArray(new String[keys.size()]);
            for (String key : array) {
                Object value = jo.get(key);
                String[] key_strs = key.split("_");
//                if (key_strs.length > 1) {
//                    StringBuilder sb = new StringBuilder();
//                    for (int i = 0; i < key_strs.length; i++) {
//                        String ks = key_strs[i];
//                        if (!"".equals(ks)) {
//                            if (i == 0) {
//                                sb.append(ks);
//                            } else {
//                                int c = ks.charAt(0);
//                                if (c >= 97 && c <= 122) {
//                                    int v = c - 32;
//                                    sb.append((char) v);
//                                    if (ks.length() > 1) {
//                                        sb.append(ks.substring(1));
//                                    }
//                                } else {
//                                    sb.append(ks);
//                                }
//                            }
//                        }
//                    }
//                    jo.remove(key);
//                    jo.put(sb.toString(), value);
//                }
                jo.remove(key);
                key = lineToHump(key);
                jo.put(key,value);
                convert(value);
            }
        }
    }

    public final static Object convert(String json) {
        Object obj = JSON.parse(json);
        convert(obj);
        return obj;
    }

    /** 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)}) */
    public static String humpToLine(String str) {
        str =  str.replaceAll("[A-Z]", "_$0").toLowerCase();
        if(str.subSequence(0,1).equals("_")){
            return str.substring(1,str.length());
        }
        return str;
    }

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine2(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String listToString(List list, char separator) {
        return StringUtils.join(list.toArray(), separator);
    }

    public static void main(String[] args) throws Exception{
        Map<String,String> params = new HashMap<>();
        params.put("a_d", "ddddd");
        params.put("b_w", "{\"s_qqq\":\"ddddddd\"}");
        params.put("ddd_d_da","[{\"dddd_eeee\":\"ddddddddddd\"},{\"iiee_eeq\":{\"eewewq_dadawdw\":\"dadwadwadawdawdwadwadwadwa\"}}]");
        //Map test = lineToHump(params);
       // JSONArray d = (JSONArray)test.get("dddDDa");
       // JSONObject s = d.getJSONObject(1);

        //System.out.println(JSON.toJSONString(s.getJSONObject("iieeEeq")));
    }
}